package cn.audit.service.user;

import cn.audit.bean.pojo.User;
import cn.audit.bean.dto.UserCorrelation;
import cn.audit.repository.user.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 李忆枫
 * @Date: 2019/5/15 14:02
 */
@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    @Transactional(rollbackFor = Exception.class)
    public User findByUsername(String username) {
        return userRepository.findUserByUsername(username);
    }

    public List<UserCorrelation> findUserAll(Integer department) {
        return userRepository.findUserAll(department);
    }

    /**
     * 保存对象
     *
     * @param user
     * @return
     * @Transactional(rollbackFor = Exception.class)
     * 如果这个类里面的方法抛出异常，就会回滚，数据库里面的数据也会回滚。
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(User user) {
        userRepository.save(user);
    }

    /**
     * 根据id查找User
     *
     * @param id
     * @return
     */
    public User findById(Long id) {
        Optional<User> op = userRepository.findById(id);
        return op.get();
    }

    /**
     * 批量更新
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(List<Long> ids,Long status) {
        userRepository.batchUpdate(ids,status);
    }

    /**
     * 重置密码
     *
     * @param ids
     * @param password
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchResetPwd(String password, List<Long> ids) {
        userRepository.batchResetPwd(password, ids);
    }

    public List<User> findById(List<Long> id) {
        return userRepository.findAllById(id);
    }

}
